#!c:\Python27\python.exe
import loggear as lg
import cv2.cv as cv
#import Image

ventanasAbiertas = []

def histograma(imagen):
    bins = 256
    hist = cv.CreateHist([bins], cv.CV_HIST_ARRAY, [[0, 255]], 1)
    cv.CalcHist([imagen], hist)
    return hist

def agregarLineasH(imagen, lista):
    h = list(lista)
    while h:
        y = h.pop()
        cv.Line(imagen, (0, y), (imagen.width, y), 255)
    return imagen

def agregarFranjasH(imagen, lista):
    h = list(lista)
    
    while h:
        y1, y2 = h.pop()
        pt1 = (0, y1)
        pt2 = (imagen.width, y2)
        cv.Rectangle(imagen, pt1, pt2, 255, -1)        
    return imagen

def agregarFranjasV(imagen, lista):
    h = list(lista)
    
    while h:
        x1, x2 = h.pop()
        pt1 = (x1, 0)
        pt2 = (x2, imagen.height)
        cv.Rectangle(imagen, pt1, pt2, 255, -1)        
    return imagen

def grisarImagen(imagen):
    out = cv.CreateImage(cv.GetSize(imagen), cv.IPL_DEPTH_8U, 1)
    cv.CvtColor(imagen, out, cv.CV_BGR2GRAY)
    return out

def lucesYSombras(imagen):
    imgGris = grisarImagen(imagen)
    (ui, us) = limitesHistograma(imgGris, 3)
    imgOsc = umbralizar(imgGris, ui, 127)
    imgClr = umbralizar(imgGris, us, 128)
    suma = cv.CreateImage(cv.GetSize(imgGris), 8, 1)
    cv.Add(imgClr, imgOsc, suma)
    return suma
    
def umbralizar(imagen, umbral, color):
    out = cv.CreateImage(cv.GetSize(imagen), 8, 1)
    cv.Threshold(imagen, out, umbral + 2, color, cv.CV_THRESH_BINARY)
    return out
    
def limitesHistograma(imagen, porcentaje):
    bins = 256
    hist = cv.CreateHist([bins], cv.CV_HIST_ARRAY, [[0, 255]], 1)
    cv.CalcHist([imagen], hist)
    
    umbral_inferior = 0
    umbral_superior = 0
    acum_inf = 0
    acum_sup = 0
    total = 0
    
    for i in range(0, bins):
        bin_val = cv.QueryHistValue_1D(hist, i)
        total += bin_val
    
    for i in range(0, bins):
        bin_val = cv.QueryHistValue_1D(hist, i)
        if acum_inf < (total * porcentaje / 100):
            acum_inf += bin_val
            umbral_inferior = i
        if acum_sup < (total * (100 - porcentaje) / 100):
            acum_sup += bin_val
            umbral_superior = i
    
    return (umbral_inferior, umbral_superior)

def areaPromContornos(contour):
    areaProm = 0
    first_c = contour
    area = cant = 0
    maximo = 0

    while contour:
        if cv.ContourArea(contour) > maximo:
            maximo = cv.ContourArea(contour)
        area += cv.ContourArea(contour)
        cant += 1
        contour = contour.h_next()
    area -= maximo
    cant -= 1
    if cant > 0:
        areaProm = area / cant
        cv.WaitKey() 
    contour = first_c 
    return (areaProm)

        
def generarPuntos(imagenSombras, umbral_pct):
    storage = cv.CreateMemStorage(0)
    contour = cv.FindContours(imagenSombras, storage, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE)
    if lg.hayLog():
        imgLog = cv.CreateImage((imagenSombras.width, imagenSombras.height), cv.IPL_DEPTH_8U, 1)
        cv.DrawContours(imgLog, contour, 0, 255, 1)
        lg.loggear('Contornos', imgLog)
    
    area = areaPromContornos(contour)
    umbral = umbral_pct / 100.00
    puntos = []
    while contour:
        if  umbral * area < cv.ContourArea(contour) < 5 * area :
            rect = cv.BoundingRect(list(contour))
            pt = (rect[0] + rect[2] / 2, rect[1] + rect[3] / 2)
            puntos.append(pt)
        contour = contour.h_next()
    return puntos
